#include   <stdio.h>
#include   <stdlib.h>
#include   <string.h>

void urlenc(char* buf, const char* str)
{
	const char special[] = "!\"#$%& '()*+,/:; <=> ?@[\\]^`{|}~% "; /* special characters */
	char tmp[10]; /* temp buffer */
	//buf[0] = '';
	//buf[0] = '\0 ';
	int i;
	for(i=0; i <strlen(str); i++)
	{
		char c = str[i];
		if(strchr(special, c)) /* special character */
		{
			sprintf(tmp, "%%%02X", (unsigned char)c);
			strcat(buf, tmp);
		}
		else if(c == ' ') /* blank */
			strcat(buf, "+ ");
		else
		{
			if(c < 0) /* none ASCII character */
			{
				sprintf(tmp, "%%%02X%%%02X", (unsigned char)str[i], (unsigned char)str[i+1]);
				strcat(buf, tmp);
				++i;
			}
			else /* ASCII character */
			{
				sprintf(tmp, "%c", c);
				strcat(buf, tmp);
			}
		}
	}
}

int main(int argc, char* argv[])
{
	//char* str = "中文 !\"#$%& '()*+,/:; <=> ?@[\\]^`{|}~%Abc ";
	//char* str = "中文";
	if (argc != 2)
	{
		printf("usage: %s string\n", argv[0]);
		exit(1);
	}
	char buf[200];
	memset(buf, 0, 200);
	urlenc(buf, argv[1]);
	printf( "%s\n", buf);
	return 0;
} 
